#ifndef VXRULLER_H
#define VXRULLER_H

#include <QtGui>
#include "vxgraphicsview.h"
#include "../States/vxmovementstate.h"
#include "../Controls/vxpinbutton.h"

class VxGraphicsView;
class VxRuller;


class VxRuller : public QFrame
{

	Q_OBJECT

public:

	enum Options{
		SNAP_DELTA	= 20,
		HEIGHT		= 18
	};

	enum Mode{
		MOVEMENT	= 0,
		STANDING	= 1
	};

	enum Orientation {
		HORIZONTAL,
		VERTICAL,
		CENTER
	};

	enum Snap {
		SNAP_TO_FRONT,
		SNAP_TO_BACK,
		NO_SNAP
	};

	VxRuller(QGraphicsView * view, bool controls=true);

	~VxRuller();

	void paintEvent(QPaintEvent * event);

	bool eventFilter(QObject *, QEvent *);

	Orientation orientation() const { return _orientation; };

	VxMovementState * buildMovementState();
	QState * buildStandingState();

	void addControlButton(QWidget * button);

	Snap snap() const { return _snap; };
	Mode mode() const { return _mode; };
	VxRuller * horzRuller() { return _info->hruller; };
	VxRuller * vertRuller() { return _info->vruller; };

	QRect getBoundingRect() const;
	
	void setClipPath(QPainterPath path){	_clipPath = path;	};

	void setSnap(Snap snap);

private:
	struct RullersInfo {
		VxRuller * center;
		VxRuller * hruller;
		VxRuller * vruller;
	};

	VxRuller(QGraphicsView * view, Orientation orientation, RullersInfo * info);

	Orientation _orientation;
	QGraphicsView * _view;
	Snap _snap;
	Mode _mode;

	RullersInfo * _info;

	QPen _metric;

	QWidget * _pointer;

	QPainterPath _clipPath;

	void _drawScale(QPainter * painter, QStyleOptionFrame * options);

	void _drawMetricHorz(QPainter * painter, qreal position, int aI, qreal aV);
	void _drawMetricVert(QPainter * painter, qreal position, int aI, qreal aV);

	void _adjustRullers();

	void _setClipRect(QPainter * painter);

	QPoint _validateXY(qreal vx, qreal vy);


	void _createGUI();
	void _createPointer();

	void _setMode(Mode mode);

	void _cursorMove(QPoint mpoint);

signals:

	void modeSwitched(int mode);
	void switchedMovementMode();
	void switchedGuidsMode();
	void snapSwitched(Snap snap);

private slots:
	void _resizeView(VxGraphicsView *);
	void _snapSwitched(Snap snap);

public slots:

	void setMovementMode() {_setMode(MOVEMENT); };
	void setGuidsMode() {_setMode(STANDING); };
	void setMode(int mode)
	{
		switch(mode){
		case 0:
			_setMode(MOVEMENT);
			break;
		case 1:
			_setMode(STANDING);
			break;
		}
	};
};




class VxRullersProxyWidget : public VxProxyWidget
{

	Q_OBJECT

public:
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

	static VxRullersProxyWidget * createInstance(VxGraphicsView * view);

private:
	VxRullersProxyWidget(VxGraphicsView * view, VxRuller * _rullers,
					  VxProxyWidget * hp, VxProxyWidget * vp, QGraphicsItem * parent=0);

	VxProxyWidget * _horzProxy;
	VxProxyWidget * _vertProxy;
	bool _alreadyMoving;

	void _updateCrossPosition();
	QPoint _validatePos(VxProxyWidget * proxy);
	void _validateSnap(VxProxyWidget * proxy);

protected:
	bool eventFilter(QObject *object, QEvent *event);
	QPoint validatePos(QPoint p);

private slots:
	void _centerMovement(int dx, int dy);
	void _checkSnap();

public slots:
	void movement(int, int);
	void cancelAnimation();
};





#endif // VXRULLER_H
